package com.vincent.redispractise.part02.Ression;

import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedissonDemo {

    @Resource
    private RedissonClient redissonClient;

    @Test
    void testRedisson(){
        RLock lock = redissonClient.getLock("anyLock");
        try {
            //waitTime：第一个参数为获取锁重试的最大的时间
            //leaseTime：第二个参数是锁超时释放时间
            boolean isLock = lock.tryLock(1, 10, TimeUnit.SECONDS);
            if (isLock) {
                try {
                    System.out.println("执行业务");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
